package com.jk.hly.controller;

import com.jk.hly.bean.HlyUser;
import com.jk.hly.service.HlyRegisterService;
import com.jk.hly.service.HlyTreeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("register")
public class HlyRegisterController {

    @Resource
    private HlyTreeService  treeService;

    @Resource
    private HlyRegisterService  registerService;


    @ResponseBody
    @RequestMapping("userRegister")
    public String userRegister(String  phone,String email, HttpSession session){
            String code = String.valueOf((int) (Math.random()*8999+1000));
            String str=registerService.userRegister(email,code);
            session.setAttribute("code",code);
            session.setAttribute("phone",phone);
            session.setAttribute("email",email);
            return  str;

    }

    @ResponseBody
    @RequestMapping("userZhuCe")
    public String userZhuCe(String code,String  phone,HttpSession session){
        String code1 = (String) session.getAttribute("code");
        HlyUser user1 = treeService.queryPhone(phone);
          if(user1!=null){
              return  "3";
          }
        if(code.equals(code1)){
            registerService.userZhuCe(phone);

            return "1";
        }
        return "2";
    }
}
